package org.liuhy.line.rect
{
	import org.liuhy.line.AbstractLine;
	import org.liuhy.node.FlowNode;
	import org.liuhy.node.focus.FocusNode;
	import org.liuhy.utils.LineUtils;
	public class RectLineUtils
	{
		public static var _LEN:int = 10;
		public function RectLineUtils()
		{			
		}
		public static function drawVirtualLine(p_focusNode:FocusNode,p_line:AbstractLine,p_lineArray:Array,p_delLineArray:Array,p_preLine:AbstractLine,p_nextLine:AbstractLine):void{
			for(var i:int=0;i<p_lineArray.length;i++){
				var _line:AbstractLine = p_lineArray[i] as AbstractLine;
				if(i+1<p_lineArray.length){
					var _line1:AbstractLine = p_lineArray[i+1] as AbstractLine;
					_line.nextLine = _line1;
					_line1.preLine = _line;
				}
				p_line.addVirtuaLine(_line);
			}
			//划虚线;
			for(var i:int=0;i<p_lineArray.length;i++){
				var _line:AbstractLine = p_lineArray[i] as AbstractLine;
				_line.virtualDraw();
			}	
		}
		public static function drawLine(p_focusNode:FocusNode,p_line:AbstractLine,p_lineArray:Array,p_delLineArray:Array,p_preLine:AbstractLine,p_nextLine:AbstractLine):void{
			for(var i:int=0;i<p_lineArray.length;i++){
				var _line:AbstractLine = p_lineArray[i] as AbstractLine;
				if(i+1<p_lineArray.length){
					var _line1:AbstractLine = p_lineArray[i+1] as AbstractLine;
					_line.nextLine = _line1;
					_line1.preLine = _line;
				}
			}
			//
			
			if(null!=p_preLine){
				(p_lineArray[0] as AbstractLine).preLine = p_preLine;
				p_preLine.nextLine = p_lineArray[0] as AbstractLine;
			}
			if(null!=p_nextLine){
				(p_lineArray[p_lineArray.length-1] as AbstractLine).nextLine = p_nextLine;
				p_nextLine.preLine = p_lineArray[p_lineArray.length-1] as AbstractLine;
			}
			for(var i:int=0;i<p_lineArray.length;i++){
				var _line:AbstractLine = p_lineArray[i] as AbstractLine;
				_line.draw();
			}
			if(null!=p_focusNode){
				//建立与箭头流程节点的关系;
				(p_lineArray[p_lineArray.length-1] as AbstractLine).in_flowFocusNode = p_focusNode;
				LineUtils.removeInLine(p_line);
				(p_focusNode.parent as FlowNode).addInLine(p_lineArray[p_lineArray.length-1]);
			}
			//重新建立与From流程节点关系						
			if(null!=p_delLineArray){
				var _frm:AbstractLine = p_delLineArray[0] as AbstractLine;
				if(null!=_frm.out_flowFocusNode){
					var _fn:FlowNode = _frm.out_flowFocusNode.parent as FlowNode;
					(p_lineArray[0] as AbstractLine).out_flowFocusNode = _frm.out_flowFocusNode;
					_fn.removeOutLine(p_delLineArray[0] as AbstractLine);
					_fn.addOutLine(p_lineArray[0] as AbstractLine);
				}
				
				for(var i:int=0;i<p_delLineArray.length;i++){
					LineUtils.removeLine(p_delLineArray[i] as AbstractLine);
				}
			}			
		}
		public static function drawFromLine(p_focusNode:FocusNode,p_line:AbstractLine,p_lineArray:Array,p_delLineArray:Array,p_preLine:AbstractLine,p_nextLine:AbstractLine):void{
			for(var i:int=0;i<p_lineArray.length;i++){
				var _line:AbstractLine = p_lineArray[i] as AbstractLine;
				if(i+1<p_lineArray.length){
					var _line1:AbstractLine = p_lineArray[i+1] as AbstractLine;
					_line.nextLine = _line1;
					_line1.preLine = _line;
				}
			}
			
			if(null!=p_preLine){
				(p_lineArray[0] as AbstractLine).preLine = p_preLine;
				p_preLine.nextLine = p_lineArray[0] as AbstractLine;
			}
			if(null!=p_nextLine){
				(p_lineArray[p_lineArray.length-1] as AbstractLine).nextLine = p_nextLine;
				p_nextLine.preLine = p_lineArray[p_lineArray.length-1] as AbstractLine;
			}
			//
			for(var i:int=0;i<p_lineArray.length;i++){
				var _line:AbstractLine = p_lineArray[i] as AbstractLine;
				_line.draw();
			}
			if(null!=p_focusNode){
				//建立与From流程节点的关系;
				(p_lineArray[0] as AbstractLine).out_flowFocusNode = p_focusNode;
				LineUtils.removeOutLine(p_line);
				(p_focusNode.parent as FlowNode).addOutLine(p_lineArray[0]);
			}		
			//重新建立与Arrow流程节点关系	
			if(null!=p_delLineArray){
				var _frm:AbstractLine = p_delLineArray[p_delLineArray.length-1] as AbstractLine;
				if(null!=_frm.in_flowFocusNode){
					var _fn:FlowNode = _frm.in_flowFocusNode.parent as FlowNode;
					(p_lineArray[p_lineArray.length-1] as AbstractLine).in_flowFocusNode = _frm.in_flowFocusNode;
					_fn.removeInLine(p_delLineArray[p_delLineArray.length-1] as AbstractLine);
					_fn.addInLine(p_lineArray[p_lineArray.length-1] as AbstractLine);
				}
				
				for(var i:int=0;i<p_delLineArray.length;i++){
					LineUtils.removeLine(p_delLineArray[i] as AbstractLine);
				}
			}			
		}
		public static function abs(p_value:int):int{
			if(p_value>=0) return p_value;
			return 0-p_value;
		}
		public static function removeLine(p_line:AbstractLine):void{
			if(null!=p_line){				
				p_line.cLine.removeChild(p_line);
			}
		}
		
		public static function removeInLine(p_uLine:AbstractLine):void{
			if(null!=p_uLine.in_flowFocusNode){
				var _flowNode:FlowNode = p_uLine.in_flowFocusNode.parent as FlowNode;
				_flowNode.removeInLine(p_uLine);
			}
		}
		public static function removeOutLine(p_uLine:AbstractLine):void{
			if(null!=p_uLine.out_flowFocusNode){
				var _flowNode:FlowNode = p_uLine.out_flowFocusNode.parent as FlowNode;
				_flowNode.removeOutLine(p_uLine);
			}
		}
//		//将三条线合并();
		private static function toPreOneLine(p_uLine:AbstractLine):void{
			//与前一线段合并;
			var _preLine:RectLine = p_uLine.preLine as RectLine;	
			if(null==_preLine) return;		
			//p_uLine是竖线;
			if( (_preLine.x1!=_preLine.x2) && abs(_preLine.x1-_preLine.x2)<_LEN){	
				//与前一线段合并;			
				if(null!=_preLine.preLine){					
					var _line_1:RectLine = new RectLine(_preLine.preLine.x1,_preLine.preLine.y1,_preLine.preLine.x1,p_uLine.y2,p_uLine.cLine);
					if(null!=_preLine.preLine.preLine){
						_preLine.preLine.preLine.nextLine = _line_1;
						_line_1.preLine = _preLine.preLine.preLine;
					}else{
						if(null!=_preLine.preLine.out_flowFocusNode){
							_line_1.out_flowFocusNode = _preLine.preLine.out_flowFocusNode;
							var _flowNode:FlowNode = _preLine.preLine.out_flowFocusNode.parent as FlowNode;
							_flowNode.removeOutLine(_preLine.preLine);
							_flowNode.addOutLine(_line_1);
						}
					}
					if(null!=p_uLine.nextLine){
						p_uLine.nextLine.x1 = _line_1.x2;
						p_uLine.nextLine.y1 = _line_1.y2;
						p_uLine.nextLine.preLine = _line_1;
						_line_1.nextLine = p_uLine.nextLine;	
						p_uLine.nextLine.draw();	
					}else{
						if(null!=p_uLine.in_flowFocusNode){
							_line_1.in_flowFocusNode = p_uLine.in_flowFocusNode;
							var _flowNode:FlowNode = p_uLine.in_flowFocusNode.parent as FlowNode;
							removeInLine(p_uLine);
							_flowNode.addInLine(_line_1);
						}
					}			
					_line_1.draw();
					//删除两根线
					removeLine(_preLine.preLine);
					removeLine(_preLine);
					removeLine(p_uLine);
				}				
			}else if((_preLine.y1!=_preLine.y2) && abs(_preLine.y1-_preLine.y2)<_LEN){
				//横线;
				if(null!=_preLine.preLine){
					var _line_1:RectLine = new RectLine(_preLine.preLine.x1,_preLine.preLine.y1,p_uLine.x2,_preLine.preLine.y1,p_uLine.cLine);
					if(null!=_preLine.preLine.preLine){
						_preLine.preLine.preLine.nextLine = _line_1;
						_line_1.preLine = _preLine.preLine.preLine;
					}
					if(null==_preLine.preLine.preLine){
						if(null!=_preLine.preLine.out_flowFocusNode){
							_line_1.out_flowFocusNode = _preLine.preLine.out_flowFocusNode;
							var _flowNode:FlowNode = _preLine.preLine.out_flowFocusNode.parent as FlowNode;
							_flowNode.removeOutLine(_preLine.preLine);
							_flowNode.addOutLine(_line_1);
						}
					}
					if(null!=p_uLine.nextLine){
						p_uLine.nextLine.x1 = _line_1.x2;
						p_uLine.nextLine.y1 = _line_1.y2;
						p_uLine.nextLine.preLine = _line_1;
						_line_1.nextLine = p_uLine.nextLine;	
						p_uLine.nextLine.draw();	
					}else{
						if(null!=p_uLine.in_flowFocusNode){
							_line_1.in_flowFocusNode = p_uLine.in_flowFocusNode;
							var _flowNode:FlowNode = p_uLine.in_flowFocusNode.parent as FlowNode;
							removeInLine(p_uLine);
							_flowNode.addInLine(_line_1);
						}
					}			
					_line_1.draw();
					//删除两根线
					removeLine(_preLine.preLine);
					removeLine(_preLine);
					removeLine(p_uLine);
				}
			}
		}
		private static function toNextOneLine(p_uLine:AbstractLine):void{
			//与前一线段合并;
			var _nextLine:RectLine = p_uLine.nextLine as RectLine;	
			if(null==_nextLine) return;		
			//p_uLine是竖线;
			if( (_nextLine.x1!=_nextLine.x2) && abs(_nextLine.x1-_nextLine.x2)<_LEN){	
				//与前一线段合并;			
				if(null!=_nextLine.nextLine){					
					var _line_1:RectLine = new RectLine(_nextLine.nextLine.x1,p_uLine.y1,_nextLine.nextLine.x2,_nextLine.nextLine.y2,p_uLine.cLine);
					if(null!=_nextLine.nextLine.nextLine){
						_nextLine.nextLine.nextLine.preLine = _line_1;
						_line_1.nextLine = _nextLine.nextLine.nextLine;
					}else{
						if(null!=_nextLine.nextLine.in_flowFocusNode){
							_line_1.in_flowFocusNode = _nextLine.nextLine.in_flowFocusNode;
							var _flowNode:FlowNode = _nextLine.nextLine.in_flowFocusNode.parent as FlowNode;
							_flowNode.removeInLine(_nextLine.nextLine);
							_flowNode.addInLine(_line_1);
						}
					}
					if(null!=p_uLine.preLine){
						p_uLine.preLine.x2		 = _line_1.x1;
						p_uLine.preLine.y2 		 = _line_1.y1;
						p_uLine.preLine.nextLine = _line_1;
						_line_1.preLine 		 = p_uLine.preLine;	
						p_uLine.preLine.draw();	
					}else{
						if(null!=p_uLine.out_flowFocusNode){
							_line_1.out_flowFocusNode = p_uLine.out_flowFocusNode;
							var _flowNode:FlowNode = p_uLine.out_flowFocusNode.parent as FlowNode;
							removeOutLine(p_uLine);
							_flowNode.addOutLine(_line_1);
						}
					}			
					_line_1.draw();
					//删除两根线
					removeLine(_nextLine.nextLine);
					removeLine(_nextLine);
					removeLine(p_uLine);
				}				
			}else if((_nextLine.y1!=_nextLine.y2) && abs(_nextLine.y1-_nextLine.y2)<_LEN){
				//横线;
				if(null!=_nextLine.nextLine){
					var _line_1:RectLine = new RectLine(p_uLine.x1,_nextLine.nextLine.y2,_nextLine.nextLine.x2,_nextLine.nextLine.y2,p_uLine.cLine);
					if(null!=_nextLine.nextLine.nextLine){
						_nextLine.nextLine.nextLine.preLine = _line_1;
						_line_1.nextLine = _nextLine.nextLine.nextLine;
					}else{
						if(null!=_nextLine.nextLine.in_flowFocusNode){
							_line_1.in_flowFocusNode = _nextLine.nextLine.in_flowFocusNode;
							var _flowNode:FlowNode = _nextLine.nextLine.in_flowFocusNode.parent as FlowNode;
							_flowNode.removeInLine(_nextLine.nextLine);
							_flowNode.addInLine(_line_1);
						}
					}
					if(null!=p_uLine.preLine){
						p_uLine.preLine.x2 			= _line_1.x1;
						p_uLine.preLine.y2 			= _line_1.y1;
						p_uLine.preLine.nextLine 	= _line_1;
						_line_1.preLine 			= p_uLine.preLine;	
						p_uLine.preLine.draw();	
					}else{
						if(null!=p_uLine.out_flowFocusNode){
							_line_1.out_flowFocusNode = p_uLine.out_flowFocusNode;
							var _flowNode:FlowNode = p_uLine.out_flowFocusNode.parent as FlowNode;
							removeOutLine(p_uLine);
							_flowNode.addOutLine(_line_1);
						}
					}			
					_line_1.draw();
					//删除两根线
					removeLine(_nextLine.nextLine);
					removeLine(_nextLine);
					removeLine(p_uLine);
				}
			}
		}
		/**
		 * 将三条线合并成一条线；
		 * p_line 当前移动线段；
		 **/
		public static function toOneLine(p_uLine:AbstractLine):void{
			if(null!=p_uLine && null!=p_uLine.preLine && null!=p_uLine.preLine.preLine){
				toPreOneLine(p_uLine);
			}else if(null!=p_uLine && null!=p_uLine.nextLine && null!=p_uLine.nextLine.nextLine){
				toNextOneLine(p_uLine);
			}
		}	
	}
}